using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using com.eforceglobal.crux.BusinessFacades;
using com.eforceglobal.crux.configuration.FeatureManager;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.exceptions;
using com.eforceglobal.crux.logging;
using com.eforceglobal.crux.logging.Enums;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.views.Enums;
using com.eforceglobal.crux.configuration;
using com.eforceglobal.crux.security;

namespace com.eforceglobal.crux.controllers.Administration
{
    public class ManageRolesController
    {
        IManageRoles _view = null;
        public ManageRolesController(IManageRoles view)
        {
            _view = view;
        }

        public void InitView(bool IsPostBack)
        {
            if (!IsPostBack)
                SwitchToListMode();
        }

        public void OpenRole(int RoleID)
        {
            RoleFacade roleFacade = new RoleFacade();
            RoleInformation roleInformation = new RoleInformation();
            PrivilageFacade privilageFacade = new PrivilageFacade();
            CompanyTypeFacade companyTypeFacade = new CompanyTypeFacade();
            roleInformation = roleFacade.GetRole(RoleID);
            if (roleInformation.IsSystemGenerated)
                NavigationController.OpenErrorPage("907");
            else
            {
                _view.AllPossiblePrivilagesToRepresent = privilageFacade.GetPrivilages();
                _view.AllPossibleCompanyTypesToRepresent = companyTypeFacade.GetAllCompanyTypes();
                roleInformation.CompanyTypes = companyTypeFacade.GetCompanyTypesForRole(RoleID);
                roleInformation.Privilages = privilageFacade.GetPrivilagesForRole(RoleID);
                _view.CurrentRole = roleInformation;
                _view.FormMode = FormMode.AddEdit;
            }
        }

        public void SwitchToListMode()
        {
            IFeatureManager featureManager = FeatureManagerFactory.GetFeatureManager();
            _view.ErrorDetails = null;
            _view.CurrentRole = new RoleInformation();
            _view.IsAddRoleSupported = featureManager.IsFeatureSupported("AddRole");
            _view.IsEditRoleSupported = featureManager.IsFeatureSupported("EditRole");
            ICruxSecurityManager securityManager = CruxSecurityManagerFactory.GetSecurityManager();
            _view.Roles = securityManager.GetAllRoles();
            //RoleFacade facade = new RoleFacade();
            //_view.Roles = facade.GetRoles();
            _view.FormMode = FormMode.List;
        }

        public void SaveRole()
        {
            try
            {
                RoleFacade roleFacade = new RoleFacade();
                roleFacade.SaveRole(_view.CurrentRole,_view.SelectedPrivilagesIdList,_view.SelectedCompanyTypesIdList);
                SwitchToListMode();
            }
            catch (CruxDatabaseException ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError(ex.ErrorNameOrCode, ex.Message);
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }

        }

        public void CancelAddEdit()
        {
            SwitchToListMode();
        }

        public void DeleteRole(int RoleID)
        {

            try
            {
                RoleFacade roleFacade = new RoleFacade();
                RoleInformation roleInformation = roleFacade.GetRole(RoleID);
                if (roleInformation.IsSystemGenerated)
                    NavigationController.OpenErrorPage("907");
                else
            
                roleFacade.DeleteRole(RoleID);
                SwitchToListMode();
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }
        }

        public RoleInformation GetRoleInformation(int concernedRoleId)
        {
            RoleFacade roleFacade = new RoleFacade();
            return roleFacade.GetRole(concernedRoleId);
        }

        public void OpenRoleAdministrationView(int RoleID)
        {
            NavigationController.OpenView("ManageRole", RoleID);
        }

        public void RedirectToPreviousPage()
        {
            NavigationController.OpenView("Administration");
        }

    }
}
